package com.dyw.ssmp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dyw.ssmp.entity.Book;
import com.dyw.ssmp.result.Result;
import com.dyw.ssmp.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Devil
 * @create 2022-01-23-23:03
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/books")
public class BookController2 {
    @Autowired
    private BookService bookService;

    @GetMapping
    public Result getAll() {
        return new Result(true, bookService.getAll());
    }

    @PostMapping
    public Result save(@RequestBody Book book) {
        return new Result(bookService.save(book));
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        return new Result(bookService.updateById(book));
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id) {
        return new Result(bookService.removeById(id));
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id) {
        return new Result(true, bookService.getById(id));
    }

//    @GetMapping("{currentPage}/{Size}")
//    public Result getPage(@PathVariable int currentPage, @PathVariable int Size) {
//        IPage<Book> iPage = new Page<>(currentPage, Size);
//        IPage<Book> page = bookService.page(iPage);
//        if(currentPage > page.getPages()){
//            IPage<Book> iPage1 = new Page<>((int)page.getPages(),Size);
//            page = bookService.page(iPage1);
//        }
//        return new Result(true, page);
//    }

    @GetMapping("{currentPage}/{Size}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int Size,Book book) {
        IPage<Book> page = bookService.getPage(currentPage,Size,book);
        if(currentPage > page.getPages()){
            page =bookService.getPage((int)page.getPages(),Size,book);
        }
        return new Result(true, page);
    }
}
